﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using InfoService.Utils;
using MediaPortal.GUI.Library;

namespace InfoService.RecentlyAdded
{
    public static class RecentlyAddedUpdater
    {
        private static readonly Logger logger = Logger.GetInstance();
        public static void SetupUpdater()
        {
            GUIWindowManager.Receivers += new SendMessageHandler(GUIWindowManager_Receivers);
        }

        private static void GUIWindowManager_Receivers(GUIMessage message)
        {
            if (message.Message == GUIMessage.MessageType.GUI_MSG_USER)
            {
                if (message.TargetWindowId == InfoServiceCore.GUIInfoServiceId)
                {
                    logger.WriteLog("Received a recently added message", LogLevel.Debug, InfoServiceModul.RecentlyAdded);
                    if (message.Object != null)
                    {
                        logger.WriteLog("Set received recenlty added message to properties", LogLevel.Info, InfoServiceModul.RecentlyAdded);
                        string[] details = (string[])message.Object;
                        string type = details[0].ToLower();
                        string Title1 = GUIPropertyManager.GetProperty("#infoservice.recentlyAdded." + type + "[1].title");
                        string Title2 = GUIPropertyManager.GetProperty("#infoservice.recentlyAdded." + type + "[2].title");
                        string Thumb1 = GUIPropertyManager.GetProperty("#infoservice.recentlyAdded." + type + "[1].thumb");
                        string Thumb2 = GUIPropertyManager.GetProperty("#infoservice.recentlyAdded." + type + "[2].thumb");
                        string Season1 = GUIPropertyManager.GetProperty("#infoservice.recentlyAdded." + type + "[1].season");
                        string Season2 = GUIPropertyManager.GetProperty("#infoservice.recentlyAdded." + type + "[2].season");
                        string Episodenumber1 = GUIPropertyManager.GetProperty("#infoservice.recentlyAdded." + type + "[1].episodenumber");
                        string Episodenumber2 = GUIPropertyManager.GetProperty("#infoservice.recentlyAdded." + type + "[2].episodenumber");
                        string Episodetitle1 = GUIPropertyManager.GetProperty("#infoservice.recentlyAdded." + type + "[1].episodetitle");
                        string Episodetitle2 = GUIPropertyManager.GetProperty("#infoservice.recentlyAdded." + type + "[2].episodetitle");
                        string Fanart1 = GUIPropertyManager.GetProperty("#infoservice.recentlyAdded." + type + "[1].fanart");
                        string Fanart2 = GUIPropertyManager.GetProperty("#infoservice.recentlyAdded." + type + "[2].fanart");

                        if (type == "series" && details.Length == 4)
                        {

                            InfoServiceUtils.SetProperty("#infoservice.recentlyAdded." + type + "[2].title", Title1);
                            InfoServiceUtils.SetProperty("#infoservice.recentlyAdded." + type + "[2].thumb", Thumb1);
                            InfoServiceUtils.SetProperty("#infoservice.recentlyAdded." + type + "[2].episodenumber", Episodenumber1);
                            InfoServiceUtils.SetProperty("#infoservice.recentlyAdded." + type + "[2].episodetitle", Episodetitle1);
                            InfoServiceUtils.SetProperty("#infoservice.recentlyAdded." + type + "[2].season", Season1);
                            InfoServiceUtils.SetProperty("#infoservice.recentlyAdded." + type + "[2].fanart", Fanart1);
                            InfoServiceUtils.SetProperty("#infoservice.recentlyAdded." + type + "[3].title", Title2);
                            InfoServiceUtils.SetProperty("#infoservice.recentlyAdded." + type + "[3].thumb", Thumb2);
                            InfoServiceUtils.SetProperty("#infoservice.recentlyAdded." + type + "[3].season", Season2);
                            InfoServiceUtils.SetProperty("#infoservice.recentlyAdded." + type + "[3].episodenumber", Episodenumber2);
                            InfoServiceUtils.SetProperty("#infoservice.recentlyAdded." + type + "[3].episodetitle", Episodetitle2);
                            InfoServiceUtils.SetProperty("#infoservice.recentlyAdded." + type + "[3].fanart", Fanart2);

                            string[] seriesInfo = details[1].Split(new[] { "/" }, StringSplitOptions.RemoveEmptyEntries);
                            InfoServiceUtils.SetProperty("#infoservice.recentlyAdded." + type + "[1].title", seriesInfo[0]);
                            InfoServiceUtils.SetProperty("#infoservice.recentlyAdded." + type + "[1].thumb", details[2]);
                            InfoServiceUtils.SetProperty("#infoservice.recentlyAdded." + type + "[1].season", seriesInfo[1]);
                            InfoServiceUtils.SetProperty("#infoservice.recentlyAdded." + type + "[1].episodenumber", seriesInfo[2]);
                            InfoServiceUtils.SetProperty("#infoservice.recentlyAdded." + type + "[1].episodetitle", seriesInfo[3]);
                            InfoServiceUtils.SetProperty("#infoservice.recentlyAdded." + type + "[1].fanart", details[3]);


                        }
                        else if (type == "movie" && details.Length == 4)
                        {
                            InfoServiceUtils.SetProperty("#infoservice.recentlyAdded." + type + "[1].title", details[1]);
                            InfoServiceUtils.SetProperty("#infoservice.recentlyAdded." + type + "[1].thumb", details[2]);
                            InfoServiceUtils.SetProperty("#infoservice.recentlyAdded." + type + "[1].fanart", details[3]);
                            InfoServiceUtils.SetProperty("#infoservice.recentlyAdded." + type + "[2].title", Title1);
                            InfoServiceUtils.SetProperty("#infoservice.recentlyAdded." + type + "[2].thumb", Thumb1);
                            InfoServiceUtils.SetProperty("#infoservice.recentlyAdded." + type + "[2].fanart", Fanart1);
                            InfoServiceUtils.SetProperty("#infoservice.recentlyAdded." + type + "[3].title", Title2);
                            InfoServiceUtils.SetProperty("#infoservice.recentlyAdded." + type + "[3].thumb", Thumb2);
                            InfoServiceUtils.SetProperty("#infoservice.recentlyAdded." + type + "[3].fanart", Fanart2);
                        }
                    }
                    else
                    {
                        logger.WriteLog("The received recently added message was empty. Skip that message", LogLevel.Warning, InfoServiceModul.RecentlyAdded);
                    }
                }
            }
        }
    }
}
